<?php

namespace app\modules\setting\models\seller;

use sookie\auth\models\Adminuser;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use app\modules\setting\models\seller\EtcSellerTiktokShop;
use yii\helpers\Json;

/**
 * EtcSellerTiktokShopSearch 是 `app\modules\setting\models\seller\EtcSellerTiktokShop` 的搜索模型.
 */
class EtcSellerTiktokShopSearch extends EtcSellerTiktokShop
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['seller_id', 'shop_code', 'shop_name', 'shop_icon', 'phone', 'phone_region_code', 'whatsapp', 'whatsapp_region_code', 'email', 'data'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // 绕过scenario()在父类中的实现
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @param integer $pageSize
     *
     * @return ActiveDataProvider
     */
    public function search($params,$select=null)
    {
        $query = EtcSellerTiktokShop::find()->select($select ? $select : "*");
        $info = Adminuser::getUserInfo();
        $query->andWhere(['seller_id' => $info->id]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

        if (!$this->validate()) {
            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
            // $query->where('0=1');
            return $dataProvider;
        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


                $query->andFilterWhere(['like', 'seller_id', $this->seller_id])                                ->andFilterWhere(['like', 'shop_code', $this->shop_code])
                                ->andFilterWhere(['like', 'shop_name', $this->shop_name])
                                ->andFilterWhere(['like', 'shop_icon', $this->shop_icon])
                                ->andFilterWhere(['like', 'phone', $this->phone])
                                ->andFilterWhere(['like', 'phone_region_code', $this->phone_region_code])
                                ->andFilterWhere(['like', 'whatsapp', $this->whatsapp])
                                ->andFilterWhere(['like', 'whatsapp_region_code', $this->whatsapp_region_code])
                                ->andFilterWhere(['like', 'email', $this->email])
                                ->andFilterWhere(['like', 'data', $this->data]);
            
        return $dataProvider;
    }
}
